package com.easylive.web.controller;

import com.easylive.web.annotation.GlobalInterceptor;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.po.VideoDanmu;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.VideoDanmuQuery;
import com.easylive.entity.request.videodanmu.WebVideoDanmuLoadDanmuReqVO;
import com.easylive.entity.request.videodanmu.WebVideoDanmuPostDanmuReqVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.service.VideoDanmuService;
import com.easylive.service.VideoInfoService;
import com.easylive.utils.StringTools;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/20 23:06
 **/
@RestController("videoDanmuController")
@RequestMapping("/danmu")
public class VideoDanmuController extends ABaseController {

    @Resource
    private VideoDanmuService videoDanmuService;

    @Resource
    private VideoInfoService videoInfoService;

    /**
     * 发弹幕
     */
    @PostMapping("/postDanmu")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO postDanmu(@Validated WebVideoDanmuPostDanmuReqVO reqVO) {

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        String userId = tokenUserInfoDto.getUserId();

        VideoDanmu videoDanmu = new VideoDanmu();
        videoDanmu.setVideoId(reqVO.getVideoId());
        videoDanmu.setFileId(reqVO.getFileId());
        videoDanmu.setUserId(userId);
        videoDanmu.setPostTime(new Date());
        videoDanmu.setText(reqVO.getText());
        videoDanmu.setMode(reqVO.getMode());
        videoDanmu.setColor(reqVO.getColor());
        videoDanmu.setTime(reqVO.getTime());

        videoDanmuService.saveVideoDanmu(videoDanmu);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadDanmu")
    public ResponseVO loadDanmu(@Validated WebVideoDanmuLoadDanmuReqVO reqVO) {

        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(reqVO.getVideoId());
        if (!ObjectUtils.isEmpty(videoInfo) && !StringTools.isEmpty(videoInfo.getInteraction()) && videoInfo.getInteraction().contains(Constants.ZERO.toString())) {
            return getSuccessResponseVO(new ArrayList<>());
        }

        VideoDanmuQuery videoDanmuQuery = new VideoDanmuQuery();
        videoDanmuQuery.setFileId(reqVO.getFileId());
        videoDanmuQuery.setVideoId(reqVO.getVideoId());
        videoDanmuQuery.setOrderBy("danmu_id asc");

        List<VideoDanmu> list = videoDanmuService.findListByParam(videoDanmuQuery);

        return getSuccessResponseVO(list);
    }



}
